﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.HtmlText.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Mvc.Extensions;

namespace MvcEngine.HtmlText.ModelBinders
{
    public class HtmlTextModelBinder : IModelBinder
    {
        #region IModelBinder Members

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;

            HtmlTextItem item = IoC.Resolve<HtmlTextItem>();
            item.Text = request.Form.Get("Content");
            item.CreatedBy = UserContext.Current.User.Id;
            item.ModifiedBy = UserContext.Current.User.Id;

            var modelState = controllerContext.Controller.ViewData.ModelState;
            modelState.AddValue("Content", request.Form.Get("Content"));

            ValidateItem(item, modelState);
            return item;
        }

        protected virtual void ValidateItem(HtmlTextItem item, ModelStateDictionary modelState)
        {
            
        }

        #endregion
    }
}
